package cn.e4j.modular.system.controller;

import cn.e4j.core.Filter.SuffixFilter;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.system.entity.SysLoad;
import cn.e4j.modular.system.entity.SysLoadlog;
import cn.e4j.modular.system.entity.SysLoadlogError;
import cn.e4j.modular.system.service.SysLoadService;
import cn.e4j.modular.system.service.SysLoadlogService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

/**
 * Load控制类
 *
 * @author yuan_rz
 * @Date 2019/03/12 18:48
 */
@Controller
@RequestMapping("/sys/load")
public class SysLoadController extends BaseController {

    @Autowired
    private SysLoadService sysLoadService;

    /**
     * 跳转页面
     */
    @RequiresPermissions("sys:load:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/load.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<SysLoad> result = sysLoadService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData addData(SysLoad sysLoad) {
        sysLoadService.addData(sysLoad);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData editData(SysLoad sysLoad) {
        sysLoadService.editData(sysLoad);
        return SUCCESS_TIP;
    }
    
    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData deleteData(SysLoad sysLoad) {
        sysLoadService.deleteData(sysLoad);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysLoad> sysLoadList = inDto.getReqList(SysLoad.class);
        sysLoadService.deleteDataBatch(sysLoadList);
        return SUCCESS_TIP;
    }

    /**
     * 获取load列表
     */
    @RequestMapping("/getLoadList")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData getLoadList(@DtoParam DtoWapper dtoWapper) {
        List<SysLoad> sysLoadList = sysLoadService.getListData(dtoWapper.getDto());
        List<Dto> list = new ArrayList<>();
        for(SysLoad sysLoad : sysLoadList){
            Dto dto = Dtos.newDto();
            dto.put("code",sysLoad.getID());
            dto.put("value",sysLoad.getLOAD_NAME());
            list.add(dto);
        }
        return ResponseData.success(list);
    }

    /**
     * 获取转换文件列表
     */
    @RequestMapping("/getFileList")
    @RequiresPermissions("sys:load:init")
    @ResponseBody
    public ResponseData getFileList() {
        File directory = new File("D:\\e4j\\kettle");
        File[] fileList = directory.listFiles(new SuffixFilter(".ktr"));
        List<Dto> list = new ArrayList<>();
        if(fileList != null){
            for(File file:fileList){
                Dto dto = Dtos.newDto();
                dto.put("code",file.getAbsolutePath());
                dto.put("value",file.getAbsolutePath());
                list.add(dto);
            }
        }

        return ResponseData.success(list);
    }

}
